<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>map demo</title>
    <link rel="stylesheet" href="../js/lib/ol4.6.4/ol.css" type="text/css">
    <link rel="stylesheet" href="../css/demo.css" type="text/css">
</head>
<body>
<p class="tool-box">
    <button id="setTarget">setTarget</button>
    <button id="setView">setView</button>
</p>
<div id="map1" class="map"></div>
<div id="map2" class="map"></div>
<script src="../js/lib/ol4.6.4/ol.js"></script>
<script src="../js/lib/jquery/jquery-3.3.1.min.js"></script>
<script>
    var view1 = new ol.View({
        zoom: 4,
        center:ol.proj.transform([110, 39],"EPSG:4326", "EPSG:3857")
    });
    var view2 = new ol.View({
        zoom: 8,
        center:ol.proj.fromLonLat([110, 39])
    });

    var target = "map1",
        view = view1;


    var map = new ol.Map({
        controls: ol.control.defaults({
            attribution: false
        }).extend([]),
        target: target,
        layers: [
            new ol.layer.Tile({
                source: new ol.source.XYZ({
                    url: "https://tile-a.openstreetmap.fr/hot/{z}/{x}/{y}.png"
                })
            })
        ],
        view: view
    });

    $("#setTarget").on("click", function () {
        if(target==="map1"){
            target = "map2";
            map.set("target", target);
        }else{
            target = "map1";
            map.setTarget(target);
        }
    });

    $("#setView").on("click", function () {
        if(view===view1){
            view = view2;
        }else{
            view = view1;
        }
        map.setView(view);
    });
</script>
</body>
</html>
